package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.UserMapper;
import com.zretc.entity.User;
import com.zretc.service.UserService;
import com.zretc.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> getUserList(Map<String, Object> param) {

        Integer pageNum = (Integer) param.get("pageNum");
        Integer pageSize = (Integer) param.get("pageSize");

        // 开始分页
        PageHelper.startPage(pageNum, pageSize);

        // 调用doa成方法
        Map<String, Object> map = new HashMap<>();
        if ("".equals(param.get("userName")) || param.get("userName") == null) {
            map.put("userName", null);
        } else {
            map.put("userName", "%" + param.get("userName") + "%");
        }

        if ("".equals(param.get("state")) || param.get("state") == null) {
            map.put("state", null);
        } else {
            map.put("state", param.get("state"));
        }

        List<User> userList = userMapper.findUserList(map);

        // 包装
        PageInfo<User> pageInfo = new PageInfo<>(userList);

        return pageInfo;
    }

    @Override
    public int updateStateByUserId(Integer state, String userId) {

        return userMapper.updateStateByUserNo(state, userId);
    }

    @Override
    public int deleteUserByUserNo(String userId) {

        return userMapper.deleteUserByUserNo(userId);
    }

    @Override
    public Boolean updateUserViewByUserId(Map<String, Object> map) {
        // 获取用户信息
        map.get("userId");
        map.get("userName");

        // 密码进行MD5加密
        String oldUserPsw = (String) map.get("userPsw");
        String userPsw = Util.getMd5(oldUserPsw);
        map.put("userPsw", userPsw);

        map.get("userPhone");
        map.get("userFace");
        map.get("state");

        int i = userMapper.updateUserViewByUserId(map);
        return i == 1 ? true : false;
    }

    @Override
    public User findUserViewByUserId(String userId) {

        return userMapper.selectUserByUserId(userId);
    }

    @Override
    public User login(String userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        List<User> userList = userMapper.findUserList(params);
        if (userList.size() != 0) {
            return userList.get(0);
        } else {
            return null;
        }
    }

    @Override
    public User findUserByCondition(Map<String, Object> param) {
        // 调用doa成方法
        Map<String, Object> map = new HashMap<>();
        if ("".equals(param.get("userName")) || param.get("userName") == null) {
            map.put("userName", null);
        } else {
            map.put("userName", param.get("userName"));
        }
        if ("".equals(param.get("userPsw")) || param.get("userPsw") == null) {
            map.put("userPsw", null);
        } else {
            map.put("userPsw", param.get("userPsw"));
        }
        if ("".equals(param.get("userPhone")) || param.get("userPhone") == null) {
            map.put("userPhone", null);
        } else {
            map.put("userPhone", param.get("userPhone"));
        }
        List<User> userByCondition = userMapper.findUserByCondition(param);
        if (userByCondition.size() != 0)
            return userByCondition.get(0);
        else
            return null;
    }

    @Override
    public int insertUser(User user) {

        return userMapper.insertUser(user);
    }
}
